package org.jeecg.modules.clues.jobs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.crawler.other.SearchNewsCrawler;
import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.clues.service.ICluesService;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecgframework.p3.core.util.HttpUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 央广网
 */
@Slf4j
public class CNRCluesJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    private ICluesService cluesService;


    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<String> keywords = cluesService.getAllKeyword();
        if(!CollectionUtils.isEmpty(keywords)){
            List<Clues> clueList = getYGWPageResponse( keywords, Integer.valueOf(parameter),"","");
            cluesService.saveOrUpdateBatch(clueList);
        }

    }


    /**
     * http://s.cnr.cn/index_sub.html?key=%E7%96%AB%E6%83%85
     * 央广网
     * @param
     * @return
     * @throws Exception
     * starttime":"2021/08/01'
     */
    public static List<Clues> getYGWPageResponse(List<String > keywords,Integer page,String startTime,String endTime) throws Exception {

        List<Clues>  cluesList = new ArrayList<>();

        if(!CollectionUtils.isEmpty(keywords)){
            for(int i =1;i<=page;i++){
                for (String keyword : keywords) {
                    HashMap<String,String> paramMap = new HashMap<>();
                    paramMap.put("page",String.valueOf(i));
                    paramMap.put("doctitle",keyword);
                    paramMap.put("starttime",startTime);
                    paramMap.put("endtime",endTime);
                    paramMap.put("size","10");

                    String s = HttpUtils.doPostJson("http://s.cnr.cn/search", JSONObject.toJSONString(paramMap));
                    JSONObject jsonObject = JSONObject.parseObject(s);
                    if(jsonObject !=null ){
                        JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString("datas"));
                        if(jsonArray !=null  && jsonArray.size()>0){
                            for (Object o : jsonArray) {
                                Clues clue = new Clues();
                                JSONObject content = JSONObject.parseObject(o.toString());
                                String articleUrl=  content.get("docpuburl").toString();
                                String source=  content.get("docsourcename").toString();
                                String pubDateStr=  content.get("crtime").toString();
                                String title =  content.get("doctitle").toString();
                                String[] split = articleUrl.split("/");

                                clue.setId("tgw"+split[split.length-1].replace(".shtml",""));
                                clue.setKeyword(keyword);
                                clue.setCluesTitle(filenameFilter(title));
                                clue.setCrawlerUrl(articleUrl);
                                clue.setCluesSource(1);
                                clue.setCrawlerSource(4);
                                cluesList.add(clue);
                            }
                        }
                    }
                }
            }
        }
        return   cluesList ;
    }




    public static String filenameFilter(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        String str = htmlStr.trim(); // 返回文本字符串
        Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]");
        str = str == null ? null : FilePattern.matcher(str).replaceAll("");
        str = str.replaceAll("\\s*", "").replaceAll("", "");
        return str;
    }





}
